package com.sun.service.impl;

import com.sun.dao.RoleMapper;
import com.sun.pojo.Role;
import com.sun.service.RoleService;
import com.sun.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    public List<Role> findRoleListByPage(RoleVo roleVo) {
        return roleMapper.findRoleListByPage(roleVo);
    }

    public int addRole(Role role) {
        return roleMapper.addRole(role);
    }

    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    public int deleteRole(int roleId) {
        return roleMapper.deleteRole(roleId);
    }

    public int deleteMenusByRoleId(int roleId) {
        return roleMapper.deleteMenusByRoleId(roleId);
    }

    public Boolean addRoleMenu(int roleId, String menuIds) {
        try {
            String[] menuIdArr = menuIds.split(",");
            /**
             * 添加菜单前先删除旧所属菜单
             */
            int deleteMenusByRoleIdResult = roleMapper.deleteMenusByRoleId(roleId);
            /**
             * 再添加新的菜单
             */
            for (int i = 0; i < menuIdArr.length; i++) {
                roleMapper.addRoleMenu(roleId,menuIdArr[i]);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<Map<String, Object>> findRoles() {
        return roleMapper.findRoles();
    }

    public List<Integer> findRoleIdsByEmployeeId(int id) {
        return roleMapper.findRoleIdsByEmployeeId(id);
    }
}
